package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.JSONUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.List;

import java.util.Map;

//该项目是符合REST规范的,此时该控制器表示操作用户的资源
@RestController //Controller+ResponseBody
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ISystemMessagesService systemMessagesService;
    @Autowired
    private IPrivateMessageService privateMessageService;
    //新增的签到service(SYM)
    @Autowired
    private ISignService signService;
    @Autowired
    private IUserVantagesService userVantagesService;
    //......................
    @Autowired
    private ITicketDetailService ticketDetailService;
    @Autowired
    private INewService newService;
    @Autowired
    private ITravelCollectionService travelCollectionService;
    @Autowired
    private IReviewService reviewService;

    /*
    更新某个用户的游记
    资源:/users/{authorId}/travels
    动作:PUT
    参数:很多...(参考数据库表)
     */
    @PutMapping("/{authorId}/travels")
    public Object updateTravels(Travel entity) {
        travelService.saveOrUpdate(entity);
        return new JSONResult();
    }

    /*
    保存某个用户的游记
    资源:/users/{authorId}/travels
    动作:POST
    参数:很多...(参考数据库表)
     */
    @PostMapping("/{authorId}/travels")
    public Object saveTravels(Travel entity) {
        //自动填充作者
        entity.setAuthor(UserContext.getCurrentUser());
        travelService.saveOrUpdate(entity);
        return new JSONResult();
    }

    /*
    获取某个用户的所有游记(带有分页功能)
    资源:/users/{authorId}/travels
    动作:GET
    参数:currentPage=x
     */
    @GetMapping("/{authorId}/travels")
    public Object queryTravel(TravelQueryObject qo) {
        //需要按照发布时间排序,这里设置下
        qo.setOrderBy("t.lastUpdateTime desc");
        qo.setState(null);
        PageInfo<Travel> info = travelService.queryTravel(qo);
        return info;
    }

    /*
        注册用户的接口
        资源:/users
        动作:POST
        参数:email=xx&nickName=xx&password=xx
     */
    @PostMapping
    public Object regist(User u) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            userService.regist(u);
            result.setData(u); //设置当前登录的用户,一起返回
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /*
    更新用户信息
    资源:/users/{id}
    动作:PUT
    参数:nickName=xx&gender=xx&place=xx&sign=xx&headImgUrl=xx&coverImgUrl=xx
     */
    @PutMapping("/{id}")
    public Object updateUser(User u) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            userService.saveOrUpdate(u);
            result.setData(u); //设置当前登录的用户,一起返回
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    /**
     * 查看当前用户的所有系统消息
     * 资源/users/{id}/systemmessages
     * 动作:GET
     * 参数:
     */
    @GetMapping("/{id}/systemmessages")
    public Object getUserMessages(SystemMessagesQueryObject qo) {
        List<SystemMessages> list = systemMessagesService.byAuthorId(qo);
        return list;
    }
    @DeleteMapping("/{id}/systemmessages")
    public void delete(@PathVariable Long id) {
        systemMessagesService.delete(id);
    }
    @PutMapping("/{id}/state")
    public void update(SystemMessagesQueryObject qo) {
        systemMessagesService.update(qo);
    }
    /**
     * 查询当前用户的私信
     * 资源
     * 动作GET
     * 参数
     */
    @GetMapping("/privatemessage")
    public Object getprivateMessage(PrivateMessageQueryObject qo) {
        List<SystemMessages> list = privateMessageService.getprivateMessage(qo);
        return list;
    } /**
     * 获取木个用户信息
     * @param
     * @return
     */
    @GetMapping
    public Object getUser(Long id) {
        return userService.get(id);
    }

    /**
     * 获取某个用户的评论
     * @param
     * @return
     */
    @GetMapping("/review")
    public Object getReview(ReviewQueryObject qo) {
        return  reviewService.getReview(qo);
    }

    /**
     * 查询那个用户@了我
     * @param name
     * @return
     */
    @GetMapping("/atuser")
    public Object getAtUser(String name) {
        String str ="@";
        if("逍遥".equals(name)){
            str=str+name;
        }else if("歪果仁".equals(name)){
            str=str+name;
        }else if("巴尔".equals(name)){
            str=str+name;
        }else if("匿名".equals(name)){
            str=str+name;
        }
        return  reviewService.getAtUser(str);
    }

    /**
     * at我小标签渲染
     * @param name
     * @return
     */
    @GetMapping("/at")
    public Object getAt(String name) {
        String str ="@";
        if("逍遥".equals(name)){
            str=str+name;
        }else if("歪果仁".equals(name)){
            str=str+name;
        }
        return  reviewService.getAt(str);
    }
    /**
     * 修改at我小标签
     * @param name
     * @return
     */
    @PutMapping("/UpdateAtreaded")
    public void updateAtreaded(String name) {
        String str ="@";
        if("逍遥".equals(name)){
            str=str+name;
        }else if("歪果仁".equals(name)){
            str=str+name;
        }
        reviewService.updateAtreaded(str);
    }
    //-------(SYM新增的)--------------
    /*
    添加某个用户的签到时间
    资源:/users/{id}/signTimes
    动作:POST
    参数:
     */
    @PostMapping(value = "/{userId}/signTimes" , produces = "text/html;charset=UTF-8")
    public Object saveSignTime(@PathVariable Long userId){
        Map<String, Object> map = signService.saveSignTime(userId);
        return JSONUtil.toJSONString(map);
    }

     /*
    查询某个用户的签到时间表
    资源:/users/{id}/signTimes
    动作:GET
    参数:
     */
     @GetMapping("/{userId}/signTimes")
     public Object listSignTime(@PathVariable Long userId){
         return signService.query(userId);
     }


    /*
  查询某个用户的积分
  资源:/users/{id}/vantages
  动作:GET
  参数:
   */
    @GetMapping("/{userId}/vantages")
    public Object getVartages(@PathVariable Long userId){
        return userVantagesService.get(userId);
    }
     //---------------------------------

    //增加用户的门票信息
    @PostMapping("/{userId}/ticketDetails")
    public Object saveTicketDetail(@PathVariable Long userId, TicketSellDetail ticketSellDetail) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            ticketDetailService.save(userId,ticketSellDetail);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
//获取用户的门票信息
    @GetMapping("/{userId}/ticketDetails")
    public Object queryTicketDetails(@PathVariable Long userId,QueryObject qo) {
        //需要按照发布时间排序,这里设置下
        qo.setOrderBy("td.buyingdate desc");
        PageInfo<TicketSellDetail> info = ticketDetailService.queryTicketDetailsByUserId(userId,qo);
        return info;
    }

    //增加用户收藏的日报
    @PutMapping("/{userId}/news")
    public Object saveNewsDetail(@PathVariable Long userId,Long newsId) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            newService.saveNewsDetailToUser(userId,newsId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //删除用户收藏的日报
    @DeleteMapping("/{userId}/news/{newsId}")
    public Object DeleteNewsDetail(@PathVariable Long userId,@PathVariable Long newsId) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            newService.deleteNewsDetailToUser(userId,newsId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //查询用户是否收藏了这个日报
    @GetMapping("/{userId}/news/{newsId}")
    public Object getNewsForUser(@PathVariable Long userId,@PathVariable Long newsId){
        JSONResult result=new JSONResult();
        String value=newService.getNewsForUser(userId,newsId);
                result.setData(value);
        return result;
    }

    //查询用户收藏的所有日报
    @GetMapping("/{userId}/news")
    public Object getNewsForUser(@PathVariable Long userId){
        List<New> news=newService.getAllNewsByUser(userId);
        return news;
    }

//---------------------------------------------------------
    //增加用户收藏的游记
    @PutMapping("/{userId}/travelCollection/{travelId}")
    public Object saveTravelCollection(@PathVariable Long userId,@PathVariable Long travelId) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            travelCollectionService.saveTravelCollectionToUser(userId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //删除用户收藏的游记
    @DeleteMapping("/{userId}/travelCollection/{travelId}")
    public Object DeleteTravelCollection(@PathVariable Long userId,@PathVariable Long travelId) {
        JSONResult result = new JSONResult();
        try {
            //调用业务层注册的方法
            travelCollectionService.deleteTravelCollectionToUser(userId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //查询用户是否收藏了这个游记
    @GetMapping("/{userId}/travelCollection/{travelId}")
    public Object getTravelCollectionForUser(@PathVariable Long userId,@PathVariable Long travelId){
        JSONResult result=new JSONResult();
        String value=travelCollectionService.getNewsForUser(userId,travelId);
        result.setData(value);
        return result;
    }

    //查询用户收藏的所有游记
    @GetMapping("/{userId}/travelCollection")
    public Object getTravelCollectionForUser(@PathVariable Long userId){
        List<TravelsCollection> travelCollections=travelCollectionService.getAllTravelCollectionsByUser(userId);
        return travelCollections;
    }
}
